package com.zixi.ai.ai.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson2.JSONObject;
import com.zixi.ai.ai.service.IVectorStoreService;
import lombok.RequiredArgsConstructor;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.springframework.ai.embedding.EmbeddingModel;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.ai.vectorstore.elasticsearch.ElasticsearchVectorStore;
import org.springframework.ai.vectorstore.elasticsearch.ElasticsearchVectorStoreOptions;
import org.springframework.ai.vectorstore.filter.FilterExpressionConverter;
import org.springframework.stereotype.Component;

/**
 * @author wfg
 */
@RequiredArgsConstructor
@Component("elasticsearchVectorStoreService")
public class ElasticsearchVectorStoreServiceImpl implements IVectorStoreService {
    @Override
    public VectorStore buildVectorStore(JSONObject config, EmbeddingModel embeddingModel, FilterExpressionConverter converter) {
        RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200)).build();
        ElasticsearchVectorStoreOptions options = new ElasticsearchVectorStoreOptions();
        BeanUtil.fillBeanWithMap(config, options, true);
        if (config.getString("vectorStoreCollection") != null) {
            options.setIndexName(config.getString("vectorStoreCollection"));
        }
        return ElasticsearchVectorStore.builder(restClient, embeddingModel).options(options).build();
    }
}
